package top.haijunit.work.modules.repo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import top.haijunit.work.core.doamin.ExceptionServer;
import top.haijunit.work.core.doamin.PageQuery;
import top.haijunit.work.core.doamin.ResultPage;
import top.haijunit.work.converter.RepoConverter;
import top.haijunit.work.modules.repo.domain.UserInfo;
import top.haijunit.work.modules.repo.entity.UserEntity;
import top.haijunit.work.modules.repo.mapper.UserMapper;

import java.util.Collection;
import java.util.List;

/**
 * @author zhanghaijun
 * @description 针对表【USERS(用户表)】的数据库操作Service实现
 * @date 2024-08-07 14:48:55
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserService extends ServiceImpl<UserMapper, UserEntity> {

    private final RepoConverter converter;

    /**
     * 根据username查询用户
     */
    public UserEntity selectUser(String username, String password) {
        Assert.hasLength(username, "登录名不能为空！");
        Assert.hasLength(password, "登录密码不能为空！");
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserEntity::getUsername, username);
        queryWrapper.eq(UserEntity::getUserPassword, password);
        return baseMapper.selectOne(queryWrapper);
    }

    private Wrapper<UserEntity> queryConditional(UserInfo userInfo) {
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (null != userInfo) {
            queryWrapper.like(CharSequenceUtil.isNotBlank(userInfo.getUsername()), UserEntity::getUsername, userInfo.getUsername());
            queryWrapper.like(CharSequenceUtil.isNotBlank(userInfo.getNickname()), UserEntity::getNickname, userInfo.getNickname());
            queryWrapper.eq(CharSequenceUtil.isNotBlank(userInfo.getEmail()), UserEntity::getEmail, userInfo.getEmail());
            queryWrapper.eq(CharSequenceUtil.isNotBlank(userInfo.getPhone()), UserEntity::getPhone, userInfo.getPhone());
            queryWrapper.eq(null != userInfo.getStatusCode(), UserEntity::getStatusCode, userInfo.getStatusCode());
        }
        queryWrapper.orderByDesc(UserEntity::getCreateTime);
        return queryWrapper;
    }

    /**
     * 获取数据分页
     */
    public ResultPage<UserInfo> selectPageList(PageQuery pageQuery, UserInfo userInfo) {
        Page<UserEntity> entityPage = this.baseMapper.selectPage(pageQuery.build(), queryConditional(userInfo));
        return ResultPage.build(entityPage, converter::convertUser);
    }

    /**
     * 获取数据列表
     */
    public Collection<UserInfo> selectList(UserInfo userInfo) {
        List<UserEntity> entityList = this.baseMapper.selectList(queryConditional(userInfo));
        return converter.convertUser(entityList);
    }

    /**
     * 获取数据详情
     */
    public UserInfo selectDetail(Long code) {
        LambdaQueryWrapper<UserEntity> query = new LambdaQueryWrapper<>();
        query.eq(UserEntity::getUniqCode, code);
        List<UserEntity> entityList = this.baseMapper.selectList(query);
        if (CollUtil.isEmpty(entityList)) {
            throw new ExceptionServer("用户不存在");
        }
        if (entityList.size() > 1) {
            log.error("数据库数据异常，存在重复的异常数据: {} 重复", code);
        }
        return converter.convert(CollUtil.getFirst(entityList));
    }

    /**
     * 添加数据
     */
    public Long insert(UserInfo dto) {
        Assert.hasLength(dto.getUsername(), "用户名不能为空！");
        if (this.exists(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, dto.getUsername()))) {
            throw new ExceptionServer("用户名已存在！");
        }
        if (CharSequenceUtil.isNotBlank(dto.getEmail()) && this.exists(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getEmail, dto.getEmail()))) {
            throw new ExceptionServer("邮箱已存在！");
        }
        if (CharSequenceUtil.isNotBlank(dto.getPhone()) && this.exists(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPhone, dto.getPhone()))) {
            throw new ExceptionServer("手机号已存在！");
        }
        UserEntity entity = converter.convert(dto);
        this.baseMapper.insert(entity);
        return entity.getUniqCode();
    }

    /**
     * 修改数据
     */
    public Long modify(UserInfo dto) {
        Assert.notNull(dto.getUserCode(), "用户Id不能为空");
        Assert.hasLength(dto.getUsername(), "用户名不能为空！");
        if (this.exists(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, dto.getUsername()).ne(UserEntity::getUniqCode, dto.getUserCode()))) {
            throw new ExceptionServer("用户名已存在！");
        }
        if (CharSequenceUtil.isNotBlank(dto.getEmail()) && this.exists(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getEmail, dto.getEmail()).ne(UserEntity::getUniqCode, dto.getUserCode()))) {
            throw new ExceptionServer("邮箱已存在！");
        }
        if (CharSequenceUtil.isNotBlank(dto.getPhone()) && this.exists(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPhone, dto.getPhone()).ne(UserEntity::getUniqCode, dto.getUserCode()))) {
            throw new ExceptionServer("手机号已存在！");
        }
        UserEntity entity = converter.convert(dto);
        Assert.notNull(entity.getUniqCode(), "用户Id不能为空");
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserEntity::getUniqCode, entity.getUniqCode());
        this.baseMapper.update(entity, wrapper);
        return entity.getUniqCode();
    }

    /**
     * 删除数据
     */
    public Long delete(Long code) {
        LambdaQueryWrapper<UserEntity> query = new LambdaQueryWrapper<>();
        query.eq(UserEntity::getUniqCode, code);
        this.baseMapper.delete(query);
        return code;
    }
}




